package com.Angaliwa
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.filters.BlurFilter;
	
	import mx.containers.Canvas;
	import mx.controls.Label;
	
	public class Toolbox
	{
		// Generates and returns a random color in #RRGGBB format.
		public static function getRandomColor(asUint:Boolean=false):*{
			var choices:Array = new Array('0', '1', '2', '3', '4', '5', '6', 
										  'A', 'B', 'C', 'D', 'E', 'F');
			
			var color:String = (asUint) ? "0x" : "#";
			for(var i:int=0; i < 6; i++){
				var found:Boolean=false;
				while(!found){
					var c:int = Math.round(Math.random()*10);
					if(c < choices.length) found=true;
				}
				
				color += choices[c];
			}
			
			return (asUint) ? uint(color) : color;
		}
		
		public static function mergeObjects(source:Object, secondary:Object):Object{
			var comb:Object = [];
			
			for(var cc:String in source){
				comb[cc] = source[cc];
			}
			
			for(var c:String in secondary){
				comb[c] = secondary[c];
			}
			
			return comb;
		}
		
		public static function applyStyles(obj:*, settings:Object):void{
			if(settings)
				for(var c:String in settings)
					obj.setStyle(c, settings[c]);
		}
		
		public static function reportDeprecatedCode(className:String, functionName:String=undefined, optionalMessage:String=undefined):void{
			var fnc:String = (functionName) ? functionName : className,
				msg:String = (optionalMessage) ? optionalMessage : 'Using deprecated code, please update your code.';
			
			trace(className.toUpperCase()+': ['+fnc+'] '+msg);
		}
		
		/* TEST, PLEASE DO NOT USE */
		public static function fullPageLoading(parentObject:*, container:*, show:Boolean=true, text:String=undefined):void{
			if(show){
				// Get a 'screenshot' of the entire application
				var br:BlurFilter = new BlurFilter(5,5);
				var parent:Sprite = Sprite(parentObject);
				var bmd:BitmapData = new BitmapData(parent.width, parent.height);
				bmd.draw(parent);
				
				var bm:Bitmap = new Bitmap(bmd);
				bm.filters = [br]; 
				bm.x = 0;
				bm.y = 0;
				
				// The container
				var cont:Canvas = new Canvas();
				cont.width = parent.width;
				cont.height= parent.height;
				cont.id,cont.name = "FullPageLoading";
				
				cont.rawChildren.addChild(bm);
				
				if(text){
					var txt:Label = new Label();
					txt.text = text;
					txt.styleName = "FullPageLoadingText";
					
					cont.addChild(txt);
					
					txt.setStyle("textAlign", "center");
					
					txt.x = (parent.width/2);//-(txt.textWidth/2);
					txt.y = (parent.height/2);//-(txt.textHeight/2);
					
					//Core.g('resizeHandler').a(txt, 'center', parentObject);
				}
				
				if(container)
					container.addChild(cont);
				else
					parentObject.addChild(cont);
			}
			else{
				if(parentObject.getChildByName("FullPageLoading")){
					parentObject.removeChild(parentObject.getChildByName("FullPageLoading"));
					
					/*
					var fd:Fade = new Fade(parentObject.getChildByName("FullPageLoading"));
					fd.duration = 300;
					fd.addEventListener(EffectEvent.EFFECT_END, function(e:EffectEvent):void{
						parentObject.removeChild(parentObject.getChildByName("FullPageLoading"));
					});
					fd.play();
					*/
				}
			}
		}
	}
}